package mk.com.ldm.corepoint.implementation.client;

import java.util.LinkedList;

import mk.com.ldm.corepoint.implementation.client.event.CoreEvent;
import mk.com.ldm.corepoint.implementation.client.event.CoreEventHandler;
import mk.com.ldm.corepoint.implementation.client.event.NavigationEvent;
import mk.com.ldm.corepoint.implementation.client.event.NavigationEventHandler;
import mk.com.ldm.corepoint.implementation.client.lib.generators.MenuGenerator;
import mk.com.ldm.corepoint.implementation.client.presenter.EditPresenter;
import mk.com.ldm.corepoint.implementation.client.presenter.EventHandler;
import mk.com.ldm.corepoint.implementation.client.presenter.LoginPresenter;
import mk.com.ldm.corepoint.implementation.client.presenter.MainPresenter;
import mk.com.ldm.corepoint.implementation.client.presenter.Presenter;
import mk.com.ldm.corepoint.implementation.client.view.EditView;
import mk.com.ldm.corepoint.implementation.client.view.LoginView;
import mk.com.ldm.corepoint.implementation.client.view.MainView;
import mk.com.ldm.corepoint.implementation.shared.LoginData;
import mk.com.ldm.corepoint.implementation.shared.MenuObject;

import org.gwt.mosaic.ui.client.MessageBox;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Hyperlink;

public class AppController implements ValueChangeHandler<String> {
	private final HandlerManager eventBus;
	private final CoreServiceAsync rpcService;
	private HasWidgets content;
	private HasWidgets header;
	private MainPresenter mp;
	private String name;
//	private MenuGenerator menuGenerator;

	public AppController(CoreServiceAsync rpcService, HandlerManager eventBus) {
		this.eventBus = eventBus;
		this.rpcService = rpcService;
		mp = new MainPresenter(rpcService, eventBus, new MainView(""),
				"WorkFlow", "Transformer");
		bind();
	}

	private void bind() {
		History.addValueChangeHandler(this);
		EventHandler.getInstance().setRpcService(rpcService);

		eventBus.addHandler(NavigationEvent.TYPE, new NavigationEventHandler() {

			public void onNavigate(NavigationEvent event) {
				if (History.getToken().split("/").length > 2) {
					launchWindow(event.getType(), event.getId(),
							event.getItemId(), name);
				} else if(!event.getName().isEmpty()){
					launchWindow(event.getType(), event.getId(),
							event.getItemId(), event.getName());
				} else {
					launchWindow(event.getType(), event.getId(),
							event.getItemId(), "");
				}

			}

		});

		eventBus.addHandler(CoreEvent.TYPE, new CoreEventHandler() {

			public void onCore(CoreEvent event) {
				EventHandler.getInstance().fireEvent(event.getType(),
						event.getCall());
			}

		});

	}

	public void go(final HasWidgets content, final HasWidgets header) {

		init(content, header);

	}

	public void onValueChange(ValueChangeEvent<String> event) {
		String token = event.getValue();
		String object = "Transformer";
		String type = "WorkFlow";
		String name = "";
		int checkSlash = token.indexOf("/");
		if (checkSlash > 0) {
			object = token.split("/")[0];
			
			type = token.split("/")[1];
			if (History.getToken().split("/").length > 2) {
				name = token.split("/")[2];
				this.name = name;
			} else {
				name = "";
			}
		}

		if (token != null) {
			Presenter presenter = null;

			mp.setObjectName(object);
			mp.setType(type);
			mp.setToken(token);
			mp.setName(name);
			MenuGenerator.getInstance().changeSection(History.getToken());

			if (!token.equals("login")) {
				presenter = mp;
			} else
				presenter = new LoginPresenter(rpcService, eventBus,
						new LoginView());

			if (presenter != null) {
				presenter.go(content);
			}
		}
	}

	public void launchWindow(String type, String objectName, String itemId,
			String name) {
		Presenter presenter;
		presenter = new EditPresenter(rpcService, eventBus, new EditView(type,
				objectName), type, objectName, itemId, name);
		presenter.go(content);
	}

	public void init(final HasWidgets _content, final HasWidgets _header) {
		rpcService.parseMenuMap(new AsyncCallback<LinkedList<MenuObject>>() {

			public void onFailure(Throwable caught) {
				MessageBox.error(
						"Server",
						"Failed to communicate with the server.<br />"
								+ caught.getMessage());
			}


			public void onSuccess(LinkedList<MenuObject> result) {
				content = _content;
				header = _header;

				if ((Cookies.getCookie("username") == null
						|| Cookies.getCookie("hash") == null)) {
					History.newItem("login");
				} else if (History.getToken().isEmpty()) {
					History.newItem("Transformer/WorkFlow");
					MenuGenerator.getInstance().create(result);
					DOM.getElementById("root").addClassName("shadow");
				} else {
					History.fireCurrentHistoryState();
					MenuGenerator.getInstance().create(result);
					DOM.getElementById("root").addClassName("shadow");
				}
				
				
			}

		});

	}
}
